/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.annotation.LoginUser;
import io.renren.common.utils.Result;
import io.renren.entity.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 测试接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/api/Utils")
@Api(tags = "测试接口")
public class ApiUtilController {

    private static final String UPLOAD_DIR = "/data/wwwroot/djltest.yixianplus.com/uploadfils";

    @PostMapping("upload")
    public Result<List<String>> uploadImages(@RequestParam("uploadImages[]") MultipartFile[] files) {
        List<String> list = new ArrayList<>();
        for (MultipartFile file : files) {
            if (file.isEmpty()) {
                continue; // next pls
            }
            try {
                String filename = file.getOriginalFilename();
                String fileUrl = "public" + File.separator + "upload" + File.separator + "imgs" + File.separator + filename;
                File destFile = new File(UPLOAD_DIR + File.separator + fileUrl);
                if (!destFile.getParentFile().exists()) {
                    destFile.getParentFile().mkdirs();
                }
                file.transferTo(destFile);
                list.add("https://djltest.yixianplus.com/uploadfils/" + fileUrl);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new Result().ok(list);
    }

    @PostMapping("getUnionidByWxOpenid")
    public Result getUnionidByWxOpenid() {


        return new Result();
    }
}